import logging
from sklearn.decomposition import PCA

# 用于对给定的权重进行主成分分析（Principal Component Analysis，PCA）降维处理
def pca(weights, components): # components为PCA 要降到的维度数
    # def flatten_weights(weights):
    #     weight_vecs = []
    #     for _, weight in weights:
    #         weight_vecs.extend(weight.flatten())
    #     return weight_vecs

    # logging.info('Flattening weights...')
    # weight_vecs = [flatten_weights(weight) for weight in weights]
    pca = PCA(components) # 创建一个 PCA 对象，并指定要降维到的维度数为 components
    new_state = pca.fit_transform(weights)
    return new_state